/**
 * 前端JavaScript配置文件
 * 统一管理API路径和应用配置
 */

// 全局配置对象
window.CONFIG = {
    // API基础路径 - 不再暴露.php后缀
    apiUrl: '/api/',
    
    // 页面路径配置
    pages: {
        home: '/',
        admin: '/admin',
        adminLogin: '/admin/login',
        adminDashboard: '/admin/dashboard',
        adminCategories: '/admin/categories',
        adminNavigations: '/admin/navigations',
        adminSettings: '/admin/settings'
    },
    
    // API端点配置 - 隐藏技术实现
    api: {
        stats: 'stats',
        search: 'search',
        navigation: 'navigation',
        category: 'category',
        upload: 'upload'
    },
    
    // 应用配置
    app: {
        name: '精美导航',
        version: '2.0.0',
        searchDebounce: 300,
        loadingTimeout: 3000,
        animationDuration: 300
    },
    
    // 搜索引擎配置
    searchEngines: {
        baidu: {
            action: 'https://www.baidu.com/s',
            param: 'wd',
            placeholder: '请您输入您要搜索的内容...',
            icon: 'B'
        },
        bing: {
            action: 'https://www.bing.com/search',
            param: 'q',
            placeholder: '请您输入您要搜索的内容...',
            icon: 'W'
        }
    },
    
    // 调试配置
    debug: {
        enabled: false,
        logLevel: 'info' // info, warn, error
    }
};

/**
 * 获取API完整URL
 * @param {string} endpoint API端点名称
 * @param {Object} params 查询参数
 * @returns {string} 完整的API URL
 */
window.CONFIG.getApiUrl = function(endpoint, params = {}) {
    const apiEndpoint = this.api[endpoint] || endpoint;
    let url = this.apiUrl + apiEndpoint;
    
    if (Object.keys(params).length > 0) {
        const queryString = new URLSearchParams(params).toString();
        url += '?' + queryString;
    }
    
    return url;
};

/**
 * 获取页面完整URL
 * @param {string} page 页面名称
 * @param {Object} params 查询参数
 * @returns {string} 完整的页面URL
 */
window.CONFIG.getPageUrl = function(page, params = {}) {
    const pagePath = this.pages[page] || '/' + page;
    let url = pagePath;
    
    if (Object.keys(params).length > 0) {
        const queryString = new URLSearchParams(params).toString();
        url += '?' + queryString;
    }
    
    return url;
};

/**
 * 调试日志输出
 * @param {string} level 日志级别
 * @param {string} message 日志消息
 * @param {*} data 额外数据
 */
window.CONFIG.log = function(level, message, data = null) {
    if (!this.debug.enabled) return;
    
    const levels = ['info', 'warn', 'error'];
    const currentLevelIndex = levels.indexOf(this.debug.logLevel);
    const logLevelIndex = levels.indexOf(level);
    
    if (logLevelIndex >= currentLevelIndex) {
        const timestamp = new Date().toISOString();
        const logMessage = `[${timestamp}][${level.toUpperCase()}] ${message}`;
        
        switch (level) {
            case 'warn':
                console.warn(logMessage, data || '');
                break;
            case 'error':
                console.error(logMessage, data || '');
                break;
            default:
                console.log(logMessage, data || '');
        }
    }
};

// 环境检测和配置调整
(function() {
    // 检测是否为开发环境
    const isDevelopment = window.location.hostname === 'localhost' || 
                         window.location.hostname === '127.0.0.1' ||
                         window.location.hostname.includes('dev');
    
    if (isDevelopment) {
        window.CONFIG.debug.enabled = true;
        window.CONFIG.debug.logLevel = 'info';
        window.CONFIG.log('info', '开发环境已启用调试模式');
    }
    
    // 检测是否为HTTPS
    if (window.location.protocol === 'https:') {
        window.CONFIG.log('info', 'HTTPS环境检测成功');
    }
    
    // 性能监控
    if ('performance' in window) {
        window.addEventListener('load', function() {
            const perfData = performance.timing;
            const loadTime = perfData.loadEventEnd - perfData.navigationStart;
            window.CONFIG.log('info', `页面加载耗时: ${loadTime}ms`);
        });
    }
})();

// 导出配置供其他模块使用
if (typeof module !== 'undefined' && module.exports) {
    module.exports = window.CONFIG;
}